package com.utc.cards.table.jade.agent.rulesAgent;

import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.utc.cards.model.card.Card;
import com.utc.cards.model.game.GameStatus;
import com.utc.cards.model.player.IPlayer;

public class ValidateCardsBehaviour extends OneShotBehaviour
{
    private static Logger log = LoggerFactory.getLogger(ValidateCardsBehaviour.class);
    private static final long serialVersionUID = 5222018002796519613L;

    private RulesAgent agent;
    private List<Card> cards;
    private IPlayer player;

    public ValidateCardsBehaviour(RulesAgent rulesAgent, List<Card> cards, IPlayer player)
    {
        super(rulesAgent);
        this.agent = rulesAgent;
        this.cards = cards;
        this.player = player;
    }

    @Override
    public void action()
    {
        log.debug("Validating cards ...");

        boolean coupValide = false;
        if (agent.getModel().getGame().getStatus().compareTo(GameStatus.PRE_GAME) == 0)
        {
            coupValide = agent.getModel().getGame().getRules().validerEchange(cards, player);
        }
        else if (agent.getModel().getGame().getStatus().compareTo(GameStatus.IN_GAME) == 0)
        {
            coupValide = agent.getModel().getGame().getRules().validerCoup(cards, player);
        }

        ACLMessage message = new ACLMessage(coupValide ? ACLMessage.AGREE : ACLMessage.REFUSE);
        message.setConversationId("validate-cards");
        message.addReceiver(agent.getModel().getPlayersMap().get(player.getName()));
        agent.send(message);
        log.debug("Validating cards DONE");
    }
}
